/*
 * CEnvenom.java
 * 
 * Copyright (c) 2009 Giles Millward (Manigra). All rights reserved.
 * 
 * This file is part of JRogueDPS.
 * 
 * JRogueDPS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * JRogueDPS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with JRogueDPS.  If not, see <http://www.gnu.org/licenses/>.
 */

package dpssystem;

import dpsstats.CDamageStats;
import dpsstats.CFinisherStats;
import dpssystem.CAttackChance.HitType;
import dpssystem.CPoison.PoisonType;

/**
 *
 * @author Giles Millward (Manigra)
 */
public class CEnvenom extends CAbility {
    CDamageStats    m_damageStats;
    CFinisherStats  m_finisherStats;

    double m_mod[] = {0.0, 0.07, 0.14, 0.21, 0.28, 0.35};
    double m_adder[] = {0, 216, 432, 648, 864, 1080};
    double m_bonusDamage = 181.0;
    boolean m_available = false;
    double m_critBoost;

    CEnvenom (CSim p_sim)
    {
        super(p_sim, false);
        m_energyRequired = 35;
        
        // Need to have deadly poison for this to work
        if (p_sim.m_player.m_MHWeapon.m_poison.m_type == PoisonType.DEADLY ||
            p_sim.m_player.m_OHWeapon.m_poison.m_type == PoisonType.DEADLY)
        {
            m_available = true;
        }

        double l_preyOnTheWeak = 1.0 + (this.m_sim.m_dpsInterface.m_talents.getPreyOnTheWeakDouble() * 4.0);
        double l_metaCritBoost = 1.0 + m_sim.m_dpsInterface.m_metaGem.getMetaGemCritBoost();
        double l_murderBoost = 1.0;

        if (m_sim.m_dpsInterface.m_target.isMurderable() && m_sim.m_dpsInterface.m_talents.getMurder() != 0)
        {
            l_murderBoost = 1.0 + 0.02; // 2% boost
        }
        
        m_damageStats = m_sim.m_stats.m_envenomStats;
        m_finisherStats = m_sim.m_stats.m_finisherStatsList.get(0);
        
        m_critBoost = 1.5 * l_preyOnTheWeak * l_metaCritBoost * l_murderBoost;

    }

    public boolean canExecute()
    {
        if (!m_available)
        {
            return false;
        }

        if (m_sim.m_player.m_energyRegen.m_currentEnergy < m_energyRequired)
        {
            // Not enough energy
            return false;
        }

        if (m_sim.m_target.m_deadlyPoisonDOT.m_stacks == 0)
        {
            // No poison doses to envenom with
            return false;
        }

        if (m_sim.m_target.m_comboPoints.m_comboPoints == 0)
        {
            // Need combo points
            return false;
        }

        if (m_sim.m_currentRunTime < m_sim.m_gcd.m_endOfGCD)
        {
            // Still on GCD
            return false;
        }

        return true;
    }

    public void execute()
    {
        // Envenom finisher (Nature damage)
        m_sim.m_gcd.startGCD();
        m_sim.m_player.m_energyRegen.useAbilityEnergy(m_energyRequired);

        // Special attack type for main hand
        HitType l_hitType = m_sim.m_attackChance.getFinisherAttackHit(m_sim.m_baseStats.getHitChance(), m_sim.m_baseStats.getExpertiseDodge());
        if (l_hitType == HitType.NORMAL)
        {
            // See if we crit with 2nd roll
            l_hitType = m_sim.m_attackChance.getSpecialAttackCrit(m_sim.m_baseStats.getCritChance());
        } else if (l_hitType == HitType.MISS || l_hitType == HitType.DODGE)
        {
            m_finisherStats.addFinisher(m_sim.m_target.m_comboPoints.m_comboPoints);
            m_sim.m_target.m_comboPoints.m_comboPoints = 0;
            m_damageStats.addStats(l_hitType, 0.0);
            // Nothing to see, we missed
            return;
        }

        double l_damage = m_adder[m_sim.m_target.m_deadlyPoisonDOT.m_stacks] +
                          m_sim.m_baseStats.getAttackPower() *
                          m_mod[m_sim.m_target.m_deadlyPoisonDOT.m_stacks];

        if (l_hitType == HitType.CRIT)
        {
            if (l_hitType == HitType.CRIT) {
                l_damage *=  m_critBoost;
            }
        }

        // spell resistance reduction;
        double l_resisted = (l_damage * 0.06);
        l_damage -= l_resisted;
        m_damageStats.addStats(l_hitType, l_damage);
        m_finisherStats.addFinisher(m_sim.m_target.m_comboPoints.m_comboPoints);

        // Update envenom buff
        this.m_endOfTimer = m_sim.m_currentRunTime + 1.0 + m_sim.m_target.m_comboPoints.m_comboPoints;
        
        m_sim.m_target.m_comboPoints.m_comboPoints = 0;
        // Consume any DP stacks
        m_sim.m_target.m_deadlyPoisonDOT.resetDOT();
        // Remove any outstanding DP tick events
        m_sim.removeEventClass(CPoisonDeadlyDOTEvent.class);

        // Proc Cut to the Chase
        m_sim.m_player.m_sliceAndDice.cutToTheChase();
        
        // Proc ruthlessness
        m_sim.m_target.m_comboPoints.ruthlessness();

        if (l_hitType == HitType.CRIT)
        {
            m_sim.m_player.m_energyRegen.focusedAttacks();
        }

        m_sim.m_player.m_energyRegen.relentlessStrikes();
    }

    public boolean hasEnvenomBuff()
    {
        return (m_sim.m_currentRunTime < this.m_endOfTimer) ? true:false;
    }
}
